package gwtmodule1.client.gui;

import gwtmodule1.client.gui.admin.PageAdminCategories;
import gwtmodule1.client.gui.admin.PageAdminIndustrialCustomer;
import gwtmodule1.client.gui.admin.PageListAccounts;
import gwtmodule1.client.gui.myaccount.PageEditAccount;
import gwtmodule1.client.gui.myaccount.PageListOrders;
import gwtmodule1.client.gui.salesdept.PageAdminGiftcards;
import gwtmodule1.client.gui.salesdept.PageAdminOrders;
import gwtmodule1.client.gui.salesdept.PageInventoryReport;
import gwtmodule1.client.gui.salesdept.PageListRetexForms;
import gwtmodule1.client.gui.salesdept.PageSalesReport;
import gwtmodule1.client.gui.stock.PageCategoryList;
import gwtmodule1.client.gui.utils.FrameClient;
import gwtmodule1.client.gui.utils.FrameProvider;
import gwtmodule1.client.gui.utils.SwitchPage;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Main extends VerticalPanel implements FrameProvider {

	private class Login extends SimplePanel implements FrameProvider {

		private final static String STYLE_LOGIN = "LoginBox";

		private final static String STYLE_EDIT = "LoginBoxEdit";

		private final static String STYLE_BUTTON = "LoginBoxButton";

		private LoginBox loginBox = new LoginBox();

		public Login() {
			super();
			setWidget(loginBox);
			setStyleName(STYLE_LOGIN);
		}

		private LogoutBox logoutBox = new LogoutBox();

		private class LogoutBox extends HorizontalPanel implements FrameClient {

			private HTML name = new HTML();

			private Button logout = new Button("Logout");

			public LogoutBox() {
				super();
				logout.addClickListener(new ClickListener() {
					public void onClick(Widget sender) {
						Login.this.setWidget(loginBox);
					}
				});
				logout.setStyleName(STYLE_BUTTON);
				add(name);
				add(logout);
			}

			public void onShow(Object param) {
				name.setText(param.toString());
			}

		}

		public SimplePanel getFrame() {
			return this;
		}

		private class LoginBox extends HorizontalPanel implements FrameClient {

			private Button login = new Button("Login");

			private PageEditAccount pageEditAccount = new PageEditAccount(0);

			private TextBox name = new TextBox();

			private Hyperlink createAccount = new Hyperlink(
					"<nobr>Create Account</nobr>", true, "CreateAccount");

			public LoginBox() {
				super();
				login.addClickListener(new ClickListener() {
					public void onClick(Widget sender) {
						Login.this.setWidget(logoutBox);
						logoutBox.name.setText(name.getText());
					}
				});
				createAccount.addClickListener(new SwitchPage(Main.this,
						pageEditAccount));
				login.setStyleName(STYLE_BUTTON);
				name.setStyleName(STYLE_EDIT);
				PasswordTextBox pwd = new PasswordTextBox();
				pwd.setStyleName(STYLE_EDIT);
				add(name);
				add(pwd);
				add(login);
				add(createAccount);
			}

			public void onShow(Object param) {
			}

		}
	}

	public Main() {
		super();
		setWidth("100%");
		add(createTop());
		add(createCenter());
	}

	private SimplePanel mainPanel;

	private class SwitchMPage extends SwitchPage {

		private boolean showLogin;

		public SwitchMPage(FrameProvider provider, FrameClient client) {
			this(provider, client, true);
		}

		public SwitchMPage(FrameProvider provider, FrameClient client,
				boolean showLogin) {
			super(provider, client);
			this.showLogin = showLogin;
		}

		public void execute() {
			if (showLogin) {
				if (menuPanel.getWidgetCount() < 2) {
					menuPanel.add(login);
					menuPanel.setCellWidth(login, "1%");
				}
			} else {
				if (menuPanel.getWidgetCount() > 1)
					menuPanel.remove(login);
			}
			super.execute();
		}

	}

	private Widget createMenu() {
		MenuBar myAcc = new MenuBar(true);
		myAcc.addItem("my Profile", new SwitchMPage(this, pageEditAccount));
		myAcc.addItem("my Orders", new SwitchMPage(this, pageListOrders));

		MenuBar sls = new MenuBar(true);
		sls.addItem("Orders", new SwitchMPage(this, pageAdminOrders));
		sls.addItem("Return/Exchange", new SwitchMPage(this, pageRetexForms));
		sls.addItem("Gift Cards", new SwitchMPage(this, pageAdminGiftcards));
		sls.addItem("Inventory Report", new SwitchMPage(this, pageStockReport));
		sls.addItem("Sales Report", new SwitchMPage(this, pageSalesReport));

		MenuBar adm = new MenuBar(true);
		adm.addItem("Accounts", new SwitchMPage(this, pageListAccounts));
		adm.addItem("Categories", new SwitchMPage(this, pageAdminCategories));
		adm.addItem("Approve Ind. Cust.", new SwitchMPage(this, pageAdminIndustrial));

		MenuBar menu = new MenuBar();
		menu.setAutoOpen(true);

		menu.addItem("Home", new SwitchMPage(this, pageHome, false));
		menu.addItem("Shop", new SwitchMPage(this, pageShop));
		menu.addItem("my Account", myAcc);
		menu.addItem("Stock", new SwitchMPage(this, pageCategoryList));
		menu.addItem("Sales Dept", sls);
		menu.addItem("Admin", adm);

		menuPanel.add(menu);
		menuPanel.add(login);
		menuPanel.setCellWidth(login, "1%");
		menuPanel.setWidth("100%");
		return menuPanel;
	}

	private HorizontalPanel menuPanel = new HorizontalPanel();

	private Login login = new Login();

	private Widget createTop() {

		HTML logo = new HTML();
		logo
				.setHTML("<div style=\"font-size:60px; text-align:right; \">QAY Online Store</div>");
		// logo.setWidth("100%");

		VerticalPanel panel = new VerticalPanel();
		// panel.setSize("100%","150px");
		panel.setWidth("100%");
		// panel.setStyleName("border");
		panel.add(logo);
		panel.add(createMenu());
		return panel;
	}

	private Widget createCenter() {
		mainPanel = new SimplePanel();
		// mainPanel.setSize("100%", "100%");
		mainPanel.setWidth("100%");
		mainPanel.setWidget(pageHome);
		// mainPanel.setStyleName("border");
		return mainPanel;
	}

	private PageHome pageHome = new PageHome();

	private PageShop pageShop = new PageShop();

	private PageEditAccount pageEditAccount = new PageEditAccount(1);

	private PageAdminOrders pageAdminOrders = new PageAdminOrders(this);

	private PageListOrders pageListOrders = new PageListOrders(this);

	private PageSalesReport pageSalesReport = new PageSalesReport();

	private PageInventoryReport pageStockReport = new PageInventoryReport();

	private PageCategoryList pageCategoryList = new PageCategoryList(this);

	private PageListAccounts pageListAccounts = new PageListAccounts(this);

	private PageAdminCategories pageAdminCategories = new PageAdminCategories(
			this);

	private PageListRetexForms pageRetexForms = new PageListRetexForms(this);

	private PageAdminGiftcards pageAdminGiftcards = new PageAdminGiftcards();
	
	private PageAdminIndustrialCustomer pageAdminIndustrial =new PageAdminIndustrialCustomer();

	public SimplePanel getFrame() {
		return mainPanel;
	}
}
